package com.mygo.caseDemo.randomLottery_D1;

/**
 * @author XMG
 * 学生类-用于构建器模式创建对象
 */
public class Student {
    /**
     * 姓名属性
     */
    private String name;
    /**
     * 年龄属性
     */
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {

    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static class Builder {
        private final Student student;

        public Builder() {
            this.student = new Student();
        }

        /**
         * 设置学生姓名
         *
         * @param name 学生的姓名，不能为 null 或空字符串
         * @return 构建器
         */
        public Builder setName(String name) {
            if (name == null || name.trim().isEmpty()) {
                throw new IllegalArgumentException("学生姓名不能为空");
            }
            student.name = name;
            return this;
        }

        /**
         * 设置学生年龄
         *
         * @param age 学生年龄不能为null或者空字符串
         * @return 构建器
         */
        public Builder setAge(int age) {
            if (age < 0) {
                throw new IllegalArgumentException("学生年龄不能为负数");
            }
            student.age = age;
            return this;
        }

        /**
         * 构件方法 可以继续添加逻辑判断如构件时必须赋值
         * @return 构件对象
         */
        public Student build() {
            return student;
        }
    }

    @Override
    public String toString() {
        return "Student{name:" + name + ",age：" + age + "}}";
    }
}
